//
//  MyJSData.m
//  QSQ
//
//  Created by zuofeng on 15/11/3.
//  Copyright © 2015年 QianShengQian, Inc. All rights reserved.
//

#import "MyJSData.h"
#import "HR-Bridging-Header.h"
#import <AdSupport/AdSupport.h>
#import <AudioToolbox/AudioToolbox.h>

@implementation MyJSData

///通过网页操纵App跳转到指定的界面
- (void)openView:(NSString*)viewName :(NSString*)params{
    NSDictionary*dic = [self stringToDicData:params];
    if ([viewName isEqualToString:@"toUrl"] || [viewName isEqualToString:@"toProduct"]) {
        if (self.delgegate && [self.delgegate respondsToSelector:@selector(handleDataWithWebJs:)]) {
            [self.delgegate handleDataWithWebJs:dic];
        }
    }
    else if ([viewName isEqualToString:@"buy"]){
        if (self.delgegate && [self.delgegate respondsToSelector:@selector(handleDataWithBuyJs:)]) {
            [self.delgegate handleDataWithBuyJs:dic];
        }
    }
    else if ([viewName isEqualToString:@"vouchers"]){
        if (self.delgegate && [self.delgegate respondsToSelector:@selector(handleDataWithVouchersJs:)]) {
            [self.delgegate handleDataWithVouchersJs:dic];
        }
    }
    else if ([viewName isEqualToString:@"currentHistory"]){
        if (self.delgegate && [self.delgegate respondsToSelector:@selector(handleDataWithCurrentHistoryJs:)]) {
            [self.delgegate handleDataWithCurrentHistoryJs:dic];
        }
    }
    else if ([viewName isEqualToString:@"book"]) {
      if (self.delgegate && [self.delgegate respondsToSelector:@selector(handleDataWithCurrentHistoryJs:)]) {
        [self.delgegate handleDataWithPreBuyJs:dic];
      }
    } else if ([viewName isEqualToString:@"addBankcard"]) {
      if (self.delgegate && [self.delgegate respondsToSelector:@selector(handleDataWithAddBankCardJs:)]) {
        [self.delgegate handleDataWithAddBankCardJs:dic];
      }
    } else if ([viewName isEqualToString:@"recharge"]) {
      if (self.delgegate && [self.delgegate respondsToSelector:@selector(handleDataWithRechargeJs:)]) {
        [self.delgegate handleDataWithRechargeJs:dic];
      }
    } else if ([viewName isEqualToString: @"hotline"]) {  // 客服
      if (self.delgegate && [self.delgegate respondsToSelector:@selector(UDesk)]) {
        [self.delgegate UDesk];
      }
    }
}

///展示能唤起客户端原生分享组件的按钮
- (void)shouldShare:(NSString*)param{
    NSDictionary*dic = [self stringToDicData:param];
    if (self.delgegate && [self.delgegate respondsToSelector:@selector(showShareWithJSData:)]) {
        [self.delgegate showShareWithJSData:dic];
    }
}

///关闭客户端原生分享组件的按钮
- (void)closeShare{
    if (self.delgegate && [self.delgegate respondsToSelector:@selector(closeShareWithJSData)]) {
        [self.delgegate closeShareWithJSData];
    }
}

///打开能唤起客户端原生分享组件的按钮
- (void)openShare:(NSString*)param{
    NSDictionary*dic = [self stringToDicData:param];
    if (self.delgegate && [self.delgegate respondsToSelector:@selector(openShareWithJSData:)]) {
        [self.delgegate openShareWithJSData:dic];
    }
}

///获取系统型号品牌等信息
- (NSString*)sysPlatform{
    NSString *deviceModel = [[UIDevice currentDevice] model];
    NSString *systemVersion = [UIDevice currentDevice].systemVersion;
    NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    NSString *userToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"x_user_token"];
    userToken = userToken ? userToken : @"";
    NSString *appID = @HRAppId;
    NSString *versionCode = @"86";
    NSString *idfa = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    NSDictionary *plant = [NSDictionary dictionaryWithObjectsAndKeys:deviceModel,@"brand",systemVersion,@"sysVersion",version,@"softVersion",userToken,@"userToken",appID,@"appID", versionCode,@"versionCode",idfa, @"xUdid",nil];
    NSError *err;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:plant options:0 error:&err];
    NSString *myString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return myString;
}

///调用系统的粘贴板
- (NSString*)setClipboard:(NSString*)content{
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = content;
    return @"0";
}

///网页内分享处理
- (void)setShareFromWeb:(NSString*)param{
    NSDictionary*dic = [self stringToDicData:param];
    if (self.delgegate && [self.delgegate respondsToSelector:@selector(ShareInWebWithJSData:)]) {
        [self.delgegate ShareInWebWithJSData:dic];
    }
}

///设置Navgation为图片
- (void)setToolBarImg{
    if (self.delgegate && [self.delgegate respondsToSelector:@selector(setToolBarImg)]) {
        [self.delgegate setToolBarImg];
    }
}

///网页内弹出登陆视图并刷新
- (void)showLogin:(NSString*)param{
  NSDictionary *dic;
  if (param != nil) {
    dic = [self stringToDicData:param];
  } else { dic = nil; }
  [[NSUserDefaults standardUserDefaults] setObject:param forKey:@"senderLogin"];
  if (self.delgegate && [self.delgegate respondsToSelector:@selector(showLoginWithJSData:)]) {
        [self.delgegate showLoginWithJSData:dic];
    }
}


- (NSString*)getLoginReferer{
    NSString *sender = [[NSUserDefaults standardUserDefaults] objectForKey:@"senderLogin"];
    return sender;
}

- (void)clearLoginReferer{
    [[NSUserDefaults standardUserDefaults] setObject:@" " forKey:@"senderLogin"];
}

///网页内部唤起短信功能
-(void)showSmsService:(NSString*)param{
    NSDictionary*dic = [self stringToDicData:param];
    if (self.delgegate && [self.delgegate respondsToSelector:@selector(showSmsWithJSData:)]) {
        [self.delgegate showSmsWithJSData:dic];
    }
}

//网页内关闭navgaition
-(void)closeTopNav{
    if (self.delgegate && [self.delgegate respondsToSelector:@selector(closeTopNav)]) {
        [self.delgegate closeTopNav];
    }
}

//网页内回到rootViewController或回到上一页
-(void)closeWebView:(NSString*)param{
  NSDictionary*dic = [self stringToDicData:param];
    if (self.delgegate && [self.delgegate respondsToSelector:@selector(popRootWithNavWithJSData:)]) {
        [self.delgegate popRootWithNavWithJSData:dic];
    }
}

///字符串类型转json
-(NSDictionary*)stringToDicData:(NSString*)param{
    NSData *data = [param dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    return dic;
}

/// 唤醒转让变现计算器
- (void)calculator:(NSString*)param {
  NSDictionary *dic = [self stringToDicData:param];
  if (self.delgegate && [self.delgegate respondsToSelector:@selector(calculator:)]) {
    [self.delgegate calculator:dic];
  }
}

/// 债权转让 取出收益
- (void)transfer:(NSString*) params {
  NSDictionary *dic = [self stringToDicData:params];
  if (self.delgegate && [self.delgegate respondsToSelector:@selector(handleDataWithTransferJs:)]) {
    [self.delgegate handleDataWithTransferJs:dic];
  }
}

/// 债权转让 取出收益
- (void)transfer2:(NSString*) params {
  NSDictionary *dic = [self stringToDicData:params];
  if (self.delgegate && [self.delgegate respondsToSelector:@selector(handleDataWithTransferJs:)]) {
    [self.delgegate handleDataWithTransferJs:dic];
  }
}


/**
 *  播放摇一摇声音
 */
- (void)shakeSound:(NSString *)beginOrEnd :(NSString *)isOn {
  NSLog(@"摇一摇声音");
  if ([beginOrEnd isEqual: @"start"]) {
    SystemSoundID ditaVoice;
    
    NSURL *shakeURl = [[NSBundle mainBundle]URLForResource:@"shake" withExtension:@"mp3"];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(shakeURl), &ditaVoice);
    AudioServicesPlaySystemSound(ditaVoice);
  }
  
  if ([beginOrEnd isEqual: @"end"]) {
    SystemSoundID endVoice;
    NSURL *endURl = [[NSBundle mainBundle]URLForResource:@"shake-end" withExtension:@"mp3"];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(endURl), &endVoice);
    AudioServicesPlaySystemSound(endVoice);
  }
}

@end
